﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;

namespace NEWS.Admin
{
    public partial class Forums : System.Web.UI.Page
    {
        private static ForumNewsBO forumnewsbo;
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                string mode = Request.QueryString["Save"];
                dldanhmuctin.DataSource = CategoryInfo.GetCategoryInfo_BySingle(false, true, false, false, false);
                dldanhmuctin.DataBind();
                getSubTopicForum(Convert.ToInt32(dldanhmuctin.SelectedValue));
                if (mode == "Complete")
                {
                    messagedelete.Text = "Đã Thêm";
                }
                dldanhmuctin.Items.Insert(0, "Tất cả");
                GridView1.DataSource = ForumNewsBOList.GetAllForumNewsBOList();
                GridView1.DataBind();
            }
        }

        protected void btndangtin_Click(object sender, EventArgs e)
        {
            Response.Redirect("PostForum.aspx?goback=true");
        }

        protected void btnxoatin_Click(object sender, EventArgs e)
        {
            bool test = false;
            foreach (GridViewRow gvRow in GridView1.Rows)
            {
                CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
                if (chkSel.Checked == true)
                {
                    try
                    {
                        test = true;
                        int forumnewsid = Convert.ToInt32(gvRow.Cells[3].Text);
                        forumnewsbo = ForumNewsBO.GetForumNewsBO(forumnewsid);
                        ForumNewsBO.DeleteForumNewsBO(forumnewsid);
                    }
                    catch
                    {
                        messagedelete.Text = "Xóa thất bại";
                        return;
                    }
                }
            }
            if (test == false)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Chưa chọn thông tin cần xóa !!!')", true);
                return;
            }
            else
            {

                dldanhmuccon_SelectedIndexChanged(sender, e);
                messagedelete.Text = "Xóa thành công! ";
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ForumNewsBO objectforum = ForumNewsBO.GetForumNewsBO(Convert.ToInt32(GridView1.SelectedRow.Cells[3].Text));
            Response.Redirect("EditForums.aspx?categoryid=" + objectforum.SubTopicForumID + "&id=" + objectforum.ForumNewID);
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //if (e.CommandName == "linktitle")
            //{
            //    string title = ((LinkButton)(((Control)e.CommandSource).Parent.Parent).FindControl("lbtlinktitle")).Text;
            //    Response.Redirect(title); 
            //}
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string strScript = "SelectDeSelectHeader(" + ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).ClientID + ");";
                    ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).Attributes.Add("onclick", strScript);
                }
            }
            catch
            {
                //report error
            }  
        }

        protected void dldanhmuctin_SelectedIndexChanged(object sender, EventArgs e)
        {
            getSubTopicForum(Convert.ToInt32(dldanhmuctin.SelectedValue));
            dldanhmuccon_SelectedIndexChanged(sender, e);
        }

        private void getSubTopicForum(int categoryid)
        {
            try
            {
                var subtopic = SubTopicForumInfo.GetSubTopicForumInfo_ByIDParrent(categoryid, false);
                dldanhmuccon.DataSource = subtopic;
                dldanhmuccon.DataBind();
                dldanhmuccon.Items.Insert(0, "Tất cả");
            }
            catch (Exception)
            { }
        }

        protected void dldanhmuccon_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dldanhmuccon.SelectedValue == "Tất cả")
            {
                GridView1.DataSource = ForumNewsBOList.GetAllForumNewsBOList();
                GridView1.DataBind();
            }
            else
            {
                GridView1.DataSource = ForumNewsBOList.GetForumNews_ByID(Convert.ToInt32(dldanhmuccon.SelectedValue));
                GridView1.DataBind();
            }
            messagedelete.Text = "";
        }

        protected void lbtdangchude_Click(object sender, EventArgs e)
        {
            if (dldanhmuctin.SelectedValue == "Tất cả")
            {
                Response.Redirect("EditCategory_Forums.aspx");
            }
            else
            {
                Response.Redirect("EditCategory_Forums.aspx?id=" + dldanhmuctin.SelectedValue + "&addsub=true" + "&back=forum");
            }
        }
    }
}
